package dynamicGui;

import gamePlay.Game;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;


import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import netcode.client.Client;

/*
 * Lobby for the host to wait until players join and start the game
 * will auto refresh but added a refresh button too.
 */


public class LobbyMenuHostE extends JPanel{
	Game game_ = MenuCyclingE.game_;
	Client client_ = MenuCyclingE.client_;

	/*
	 * Begin Constructors---------------------------------------------------
	 */
	//default constructor
	//Constructor that also takes in the game object
	public LobbyMenuHostE() {
        super();
        System.out.println("DEBUG: Creating lobbyMenuHost");

        this.setLayout(new GridLayout(0,2, 0, 10));


        createPlayerNameTextFields();//create a list of all players, and kick buttons
        addStartGameButton();
        addLeaveButton();
        addRefreshButton();
        addFAQButton();

//        timerThing_ = new TimerThing(5, this);
        new TimerObjectSet(3, this);//set up the timer to refresh the screen in 2 seconds
	}
    
    /*
	 * End Constructors------------------------------------------------------------------------------------------------
	 */
	
	
	
	
	
	
    /*
     * Begin button Creation Methods---------------------------------------------------------------------------------
     */
	
	//Add Kick button
	private void addKickButton(final String playerToBeKicked){
		JButton kickButton = new JButton("Kick");
		kickButton.setPreferredSize(new Dimension(200, 30));
    	this.add(kickButton);
    	kickButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	kickButtonActionPerformed(evt, playerToBeKicked);
            }
        });
	}

	//Add startGame button
		private void addStartGameButton(){
			JButton startGameButton = new JButton("StartGame");
			startGameButton.setPreferredSize(new Dimension(200, 30));
	    	this.add(startGameButton);
	    	startGameButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	            	startGameButtonActionPerformed(evt);
	            }
	        });
		}
	
	//Add LeaveButton
	private void addLeaveButton(){
		JButton leaveButton = new JButton("LeaveCurrentGame");
    	leaveButton.setPreferredSize(new Dimension(200, 30));
    	this.add(leaveButton);
    	leaveButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	leaveButtonActionPerformed(evt);
            }
        });
	}
	
	
	//ADD in a refresh button
    private void addRefreshButton() {
    	JButton refreshButton = new JButton("REFRESH");
    	refreshButton.setPreferredSize(new Dimension(200, 30));
    	this.add(refreshButton);
    	refreshButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	refreshButtonActionPerformed(evt);
            }
        });
	}
    
	//Add FAQButton
	private void addFAQButton(){
		JButton refreshButton = new JButton("FAQ");
    	refreshButton.setPreferredSize(new Dimension(200, 30));
    	this.add(refreshButton);
    	refreshButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	FAQButtonActionPerformed(evt);
            }
        });
	}
	
    /*
     * End button Creation Methods------------------------------------------------------------------------------------------
     */

	
	
	
	
	
	
	
	
	
	
    /*
     * Begin textFields Creation Methods------------------------------------------------------------------------------------------
     */
	//CreateTextfields of the players in the current game
	//NOTE: since this is a host, add kick button after the text field
	private void createPlayerNameTextFields(){
		for (int i = 0; i < game_.getPlayers().size(); i++) {
	    	JLabel playerNameTextField = new JLabel("PlayerName: " +game_.getPlayers().get(i).getPlayer_name());
	    	playerNameTextField.setPreferredSize(new Dimension(200, 30));
	    	this.add(playerNameTextField);   	
	    	addKickButton(game_.getPlayers().get(i).getPlayer_name());
		}
	}
    /*
     * End textFields Creation Methods------------------------------------------------------------------------------------------
     */
	
	
	
	
	
	
    /*
     *ActionListeners begin--------------------------------------------------------------------------------------------------
     */
	//Pressing leavegame Button
	private void leaveButtonActionPerformed(java.awt.event.ActionEvent evt) {
		System.out.println("Sending leave game request: " +game_.getGameID());
		client_.sendLeaveGameRequest(game_.getGameID());
		//goes back to the lobby selection menu
		MenuCyclingE.displayLobbySelectionMenu();
	}
	

	//Pressing a kick Button
	private void kickButtonActionPerformed(java.awt.event.ActionEvent evt, String playerNameToBeKicked) {
		//How to kick someone? No idea....
		//TODO kick?
		
		System.out.println("DEBUG: Kicking some player... needs to be implemented: " +playerNameToBeKicked);
		
	}
	private void startGameButtonActionPerformed(java.awt.event.ActionEvent evt){
		System.out.println("DEBUG: Sending startGameRequest " +game_.getGameID());
		client_.sendStartGameRequest(game_.getGameID());
		//Starting the game
		MenuCyclingE.displayQuestionMenu();//starting the game means go to the first question, not MenuCyclingE.displayStartGameMenu() "which does not exist..."
	}

    //Pressing the refresh button, deletes the current gui and recreates it
    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	System.out.println("DEBUG: Refresh Button Pressed");
    	refresh();
        
     }
    //encapsulating the refresh method
    private void refresh(){
//    	System.out.println("DEBUG: REFRESHING");
    	this.removeAll();
    	this.setLayout(new GridLayout(0,2, 0, 10));
    	createPlayerNameTextFields();//create a list of all players, and kick buttons
        addStartGameButton();
        addLeaveButton();
        addRefreshButton();
        addFAQButton();
        this.validate();// re-layout everything in this panel
        
        new TimerObjectSet(2, this); //refresh again in 2 seconds
    }
    
	//Pressing FAQ Button
	private void FAQButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_QuickGameButtonActionPerformed
		try {
	        //Set your page url in this string. For eg, I m using URL for Google Search engine
	        String url = "http://homepages.uwp.edu/dalby001/faq.html";
	        java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
	      }
	      catch (java.io.IOException e) {
	          System.out.println(e.getMessage());
	      }
	}
    /*
     *ActionListeners end--------------------------------------------------------------------------------------------------
     */

    
    //Inner Class to create a timer to refresh
	public class TimerObjectSet {
	  Toolkit toolkit;
	
	  Timer timer;
	
	  public TimerObjectSet(int seconds, LobbyMenuHostE lobbyMenuHostE) {
	    toolkit = Toolkit.getDefaultToolkit();
	    timer = new Timer();
	    timer.schedule(new RemindTask(lobbyMenuHostE), seconds * 1000);
	    
	  }
	
	  //after time is up, do this TODO
	  class RemindTask extends TimerTask {
		LobbyMenuHostE lobbyMenuHostE_;
	    public RemindTask(LobbyMenuHostE inlobbyMenuHostE) {
	    	lobbyMenuHostE_ = inlobbyMenuHostE;
			// TODO Auto-generated constructor stub
		}

		public void run() {
	    	//Refresh the screen to see if we have new players that have joined the lobby
			lobbyMenuHostE_.refresh();
//			System.out.println("DEBUG: Refreshing: timer event");
	    }
	  }
	}
}
